package com.beagle.njddapp.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.beagle.njddapp.utils.LocalDisplay;


public class SwitchButton extends View {
    private final Paint mPaint = new Paint();
    private final Paint mBorderPaint = new Paint();
    private static final double MBTNHEIGHT = 0.7;
    private static final int OFFSET = LocalDisplay.dp2px(2);
    private static final int OFFSET_STORKEN = 6;
    private int mHeight;
    private float mAnimate = 0L;
    private boolean checked = false;
    private float mScale;
    private OnCheckedChangeListener mOnCheckedChangeListener;
    private RectF mRectFInner;
    private RectF mRectFBg;

    public SwitchButton(Context context) {
        this(context, null);
    }

    public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    /**
     *      * @param widthMeasureSpec
     *      * @param heightMeasureSpec 高度是是宽度的0.55倍
     *     
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = (int) (MBTNHEIGHT * width);
        setMeasuredDimension(width, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.WHITE);
        if (mRectFBg == null) {
            mRectFBg = new RectF();
        }
        mRectFBg.left = OFFSET_STORKEN;
        mRectFBg.top = OFFSET_STORKEN;
        mRectFBg.right = getWidth() - OFFSET_STORKEN;
        mRectFBg.bottom = getHeight() - OFFSET_STORKEN;
        //以下save和restore很重要，确保动画在中间一层 ，如果大家不明白，可以去搜下用法
        canvas.save();
//        mPaint.setColor(mUnCheckedBorderColor);
        mAnimate = mAnimate - 0.1f > 0 ? mAnimate - 0.1f : 0; // 动画标示 ，重绘10次，借鉴被人的动画
        mScale = (!checked ? 1 - mAnimate : mAnimate);
        canvas.scale(mScale, mScale, getWidth() - getHeight() / 2, mRectFBg.centerY());
//        //绘制缩放的灰色圆角矩形
//        canvas.drawRoundRect(mRectFBg, mHeight / 2, mHeight / 2, mBorderPaint);

        //绘制缩放的圆角矩形，和上边的重叠实现边框效果
        mPaint.setColor(Color.WHITE);
        if (mRectFInner == null) {
            mRectFInner = new RectF();
        }
        mRectFInner.left = OFFSET;
        mRectFInner.top = OFFSET;
        mRectFInner.right = getWidth() - OFFSET;
        mRectFInner.bottom = getHeight() - OFFSET;
        canvas.drawRoundRect(mRectFInner, (mHeight - 8) / 2, (mHeight - 8) / 2, mPaint);
        canvas.restore();
        canvas.save();
        //中间圆形平移
        int sWidth = getWidth();
        int bTranslateX = sWidth - getHeight();
        final float translate = bTranslateX * (!checked ? mAnimate : 1 - mAnimate);
        canvas.translate(translate, 0);

        //以下两个圆带灰色边框
//        mPaint.setColor(mCircleIndicatorBorderColor);
//        canvas.drawCircle(getHeight() / 2, getHeight() / 2, getHeight() / 2 - OFFSET / 2, mPaint);
        if (checked){
            mPaint.setShadowLayer(0.5f,1,0, Color.parseColor("#0049FF"));
        }else {
            mPaint.setShadowLayer(0.5f,-1,0, Color.parseColor("#440092CB"));
        }
        if (isChecked()){
            mPaint.setColor(Color.parseColor("#0049FF"));
        }else {
            mPaint.setColor(Color.parseColor("#cccccc"));
        }
        canvas.drawCircle(getHeight() / 2, getHeight() / 2, getHeight() / 2 - OFFSET-12, mPaint);

        canvas.restore();
        if (isChecked()){
            mBorderPaint.setColor(Color.parseColor("#0049FF"));
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setStrokeWidth(3);
        }else {
            mBorderPaint.setColor(Color.parseColor("#cccccc"));
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setStrokeWidth(3);
        }
        canvas.drawRoundRect(mRectFBg, mHeight / 2, mHeight / 2, mBorderPaint);
        if (mScale > 0) {
            mPaint.reset();
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return true;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (mAnimate==1){
                    mAnimate=0;
                }else if (mAnimate==0){
                    mAnimate = 1;
                }
                checked = !checked;
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.OnCheckedChanged(checked);
                }
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener mOnCheckedChangeListener) {
        this.mOnCheckedChangeListener = mOnCheckedChangeListener;
    }

    public interface OnCheckedChangeListener {
        void OnCheckedChanged(boolean isChecked);
    }
}